package cz.proks.easyhelpdesk.utils;

import java.util.HashMap;

import cz.proks.easyhelpdesk.api.config.Configuration;

public class UrlUtils {
            
    public static HashMap<String, String> getUrlParameters(String url) {
        HashMap<String, String> parameterMap = new HashMap<String, String>();

        if (url == null)
            return parameterMap;

        int questionMark = url.indexOf('?');

        if (questionMark < 0 || questionMark == url.length())
            return parameterMap;

        String lastPart = url.substring(questionMark + 1);

        String[] splitted = lastPart.split("&");

        for (int i = 0; i < splitted.length; i++) {
            String[] keyValue = splitted[i].split("=");

            String key = null;
            String value = null;
            
            if (keyValue.length == 1) 
                key = keyValue[0];                            
            
            else if (keyValue.length == 2) {
                key = keyValue[0];
                value = keyValue[1];
            }
            
            else if (keyValue.length != 2)
                throw new IllegalArgumentException("Unexpected pair: " + splitted[i]);
            

            parameterMap.put(key, value);
        }

        return parameterMap;
    }

    public static String getUrlParameter(String url, String parameter) {
        return getUrlParameters(url).get(parameter);
    }

    public static String getAuthorizationCode(String url) {
        return getUrlParameter(url, "code");
    }

    public static String getAuthorizationError(String url) {
        return getUrlParameter(url, "error");
    }    
    
    public static String getAccessToken(String url) {
        return getUrlParameter(url, "accessToken");
    }

    public static String scopesToString(String[] scopes) {
        if (scopes == null || scopes.length == 0)
            return "";

        String separator = " ";
        boolean needSeparator = false;
        StringBuilder builder = new StringBuilder();

        for (String scope : scopes) {
            if (needSeparator)
                builder.append(separator);
            else
                needSeparator = true;

            builder.append(scope);
        }

        return builder.toString();
    }

    public static String getGoogleOAuthUrl(String clientId, String responseType, String[] scopes, String redirectUri, String accessType, String state) {
        StringBuilder builder = new StringBuilder();
        builder.append(Configuration.GWT.GOOGLE_OAUTH.OAUTH2_ENDPOINT).append("?").append("client_id").append("=").append(clientId)
                .append("&").append("response_type").append("=").append("code").append("&").append("scope").append("=").append(scopesToString(scopes)).append("&")
                .append("redirect_uri").append("=").append(Configuration.GWT.OAUTH2_REDIRECT_URI).append("&").append("access_type").append("=")
                .append("online").append("&").append("state").append("=").append(state);

        return builder.toString();
    }
    
    public static String appendQuery(String url, String name, Object value) {
        StringBuilder builder = new StringBuilder(url);
        builder.append(url.contains("?") ? "&" : "?").append(name).append("=").append(value.toString());
        return builder.toString();            
    }        
}
